#include "lv_task.h"
#include "lv_mainstart.h"

lv_obj_t* obj1 = NULL;
lv_obj_t* obj2 = NULL;

void obj1_event_cb(lv_event_t* event)
{
    lv_event_code_t code = lv_event_get_code(event);
    lv_obj_t* target = lv_event_get_target(event);

    if(target == obj1) {
        switch(code) {
            case LV_EVENT_PRESSED:
                printk("===obj1_LV_EVENT_PRESSED_DOWN===\r\n");
            break;

            case LV_EVENT_RELEASED:
                printk("===obj1_LV_EVENT_RELEASED_UP===\r\n");
            break;
        }
    } else if (target == obj2) {
        switch(code) {
            case LV_EVENT_PRESSED:
                printk("===obj2_LV_EVENT_PRESSED_DOWN===\r\n");
            break;

            case LV_EVENT_RELEASED:
                printk("===obj2_LV_EVENT_RELEASED_UP===\r\n");
            break;
        }
    }
    
}

void my_gui(void)
{
    /* 创建一个样式 */
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_bg_color(&style, lv_color_hex(0xe06c63));

    // lv_obj_t* obj1 = lv_slider_create(lv_scr_act());
    // lv_obj_set_size(obj1, 50, 10);

    // lv_obj_align(obj1, LV_ALIGN_CENTER, 0, 0);
    // lv_obj_add_style(obj1, &style, LV_STATE_DEFAULT | LV_PART_INDICATOR);

    // lv_obj_set_style_bg_color(obj1, lv_color_hex(0x282c34), LV_STATE_DEFAULT | LV_PART_KNOB);

    // lv_slider_set_range(obj1, 0, 2);
    // lv_slider_set_value(obj1, 1, LV_ANIM_ON);

    obj1 = lv_obj_create(lv_scr_act());
    lv_obj_set_size(obj1, 120, 60);
    lv_obj_align(obj1, LV_ALIGN_LEFT_MID, 0, 0);

    lv_obj_add_event_cb(obj1, obj1_event_cb, LV_EVENT_PRESSED, NULL);
    lv_obj_add_event_cb(obj1, obj1_event_cb, LV_EVENT_RELEASED, NULL);

    obj2 = lv_obj_create(lv_scr_act());
    lv_obj_set_size(obj2, 120, 60);
    lv_obj_align(obj2, LV_ALIGN_RIGHT_MID, 0, 0);

    lv_obj_add_event_cb(obj2, obj1_event_cb, LV_EVENT_PRESSED, NULL);
    lv_obj_add_event_cb(obj2, obj1_event_cb, LV_EVENT_RELEASED, NULL);
    
    // lv_obj_set_size(obj1, 300, 200);
    // lv_obj_align(obj1, LV_ALIGN_CENTER, 0, 0);
    
    // lv_obj_set_style_bg_color(obj1, lv_color_hex(0x46a3ef), LV_STATE_PRESSED);

    // lv_obj_t* obj2 = lv_obj_create(lv_scr_act());
    
    // lv_obj_set_size(obj2, 60, 60);
    // lv_obj_align_to(obj2, obj1, LV_ALIGN_CENTER, 0, 0);
    
    // lv_obj_add_style(obj2, &style, LV_STATE_PRESSED);
    // lv_obj_set_style_border_color(obj2, lv_color_hex(0x98c379), LV_STATE_DEFAULT);
    // lv_obj_set_style_border_width(obj2, 1, LV_STATE_PRESSED);
}

/**
 * @brief       LVGL运行例程
 * @param       pvParameters : 传入参数(未用到)
 * @retval      无
 */
void lv_task_main(void *pvParameters)
{
    pvParameters = pvParameters;

    lv_init();                                          /* lvgl系统初始化 */
    lv_port_disp_init();                                /* lvgl显示接口初始化,放在lv_init()的后面 */
    lv_port_indev_init();                               /* lvgl输入接口初始化,放在lv_init()的后面 */

    uart_print_mode_set(TRACE_MODE_DMA);

    lv_mainstart();

    printk("my_SRAMEX_perused: %d\r\n", my_mem_perused(SRAMEX));
    printk("my_SRAMIN_perused: %d\r\n", my_mem_perused(SRAMIN));
    
    while(1)
    {   

        lv_timer_handler(); /* LVGL计时器 */
        printk_handler();
        vTaskDelay(5);
    }
}

